package com.magic.gre.ui.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.magic.gre.R;
import com.magic.gre.base.fragment.BaseFragment;
import com.magic.gre.entity.SerializableMap;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.net.RxBus;
import com.noname.lib_base_java.util.TimeUtils;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-11 18:14
 * 文件名: Guide5Fragment
 * 描述:
 */
public class Guide5Fragment extends BaseFragment {

    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.content_tv)
    TextView contentTv;
    @BindView(R.id.next_tv)
    TextView nextTv;

    public static Guide5Fragment newInstance(SerializableMap serializableMap) {
        Guide5Fragment fragment = new Guide5Fragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("parmsMap", serializableMap);
        fragment.setArguments(bundle);
        return fragment;
    }

    private SerializableMap serializableMap;
    private TimePickerView birthTime;
    private long time;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_guide_5;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        bt1.setSelected(true);
        serializableMap = (SerializableMap) getArguments().getSerializable("parmsMap");

        contentTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s==null||s.length()==0){
                    nextTv.setEnabled(false);
                }else {
                    nextTv.setEnabled(true);
                }

            }
        });
    }

    @OnClick(value = {R.id.skip_bt, R.id.next_tv,R.id.content_tv})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.skip_bt:
                send();
                break;
            case R.id.content_tv:
                if (birthTime == null) {
                    initCustomOptionPicker();
                }
                birthTime.show();

                break;
            case R.id.next_tv:
                serializableMap.getMap().put("nextTime", time);
                send();
                break;

        }
    }

    private void initCustomOptionPicker() {
        birthTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                time=date.getTime();
//                time=TimeUtils.long2String(date.getTime(), TimeUtils.FORMAT_YMD_ONE);
                serializableMap.getMap().put("nextTime", TimeUtils.long2String(date.getTime(), TimeUtils.FORMAT_YMD_ONE));
                contentTv.setText(TimeUtils.long2String(date.getTime(), TimeUtils.FORMAT_YMD_ONE));
            }
        }).setGravity(Gravity.CENTER)
                .setTextColorCenter(ContextCompat.getColor(mContext,R.color.main_color))
                .setTextColorOut(ContextCompat.getColor(mContext,R.color.color_666666))
                .setCancelColor(ContextCompat.getColor(mContext,R.color.main_color))
                .setSubmitColor(ContextCompat.getColor(mContext,R.color.main_color))
                .setBgColor(ContextCompat.getColor(mContext ,R.color.color_FFFFFF))
                .isCenterLabel(true)
                .build();
    }

    private void send() {
        MsgEvent msgEvent = new MsgEvent(MsgEvent.GUIDE_NEXT);
        msgEvent.put("next", 5);
        RxBus.getInstance().post(msgEvent);
    }
}
